package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-24
 * Time：21:27
 */
public class AdminDao {
    public Admin selectByName(String adminName) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from admin where adminName = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, adminName);
            resultSet  = statement.executeQuery();
            if (resultSet.next()) {
                Admin admin = new Admin();
                admin.setAdminId(resultSet.getInt("adminId"));
                admin.setAdminName(resultSet.getString("adminName"));
                admin.setPassword(resultSet.getString("password"));
                return admin;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
}
